//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmLogBasketInnerValue.h"
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpIterator.h>
#include <gen/QL/Instruments/BasketPayoff.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmInnerValueCalculator.h>
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (Cephei::QL::Instruments::IBasketPayoff^ payoff, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher) : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
    CBasketPayoff^ _Cpayoff;
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phFdmLogBasketInnerValue = NULL;
#endif
        _Cpayoff = safe_cast<CBasketPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::BasketPayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::BasketPayoff>&> (_Cpayoff->GetShared ()); 
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ppFdmLogBasketInnerValue = new boost::shared_ptr<QuantLib::FdmLogBasketInnerValue> (new QuantLib::FdmLogBasketInnerValue ( _payoff,  _mesher ));
        SetFdmInnerValueCalculator (boost::dynamic_pointer_cast<QuantLib::FdmInnerValueCalculator> (*_ppFdmLogBasketInnerValue));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (boost::shared_ptr<QuantLib::FdmLogBasketInnerValue>& childNative, Object^ owner) : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
#ifdef HANDLE
	_phFdmLogBasketInnerValue = NULL;
#endif
	_ppFdmLogBasketInnerValue = &childNative;
    _ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (boost::dynamic_pointer_cast<QuantLib::FdmInnerValueCalculator> (*_ppFdmLogBasketInnerValue));
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (QuantLib::FdmLogBasketInnerValue& childNative, Object^ owner) : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
#ifdef HANDLE
	_phFdmLogBasketInnerValue = NULL;
#endif
	_ppFdmLogBasketInnerValue = new boost::shared_ptr<QuantLib::FdmLogBasketInnerValue> (&childNative);
    _ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (boost::dynamic_pointer_cast<QuantLib::FdmInnerValueCalculator> (*_ppFdmLogBasketInnerValue));
    _FdmLogBasketInnerValueOwner = owner;
    _FdmInnerValueCalculatorOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (CFdmLogBasketInnerValue^ copy) : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
#ifdef HANDLE
	_phFdmLogBasketInnerValue = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmLogBasketInnerValue = new boost::shared_ptr<QuantLib::FdmLogBasketInnerValue> (copy->GetShared());
        _ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (boost::dynamic_pointer_cast<QuantLib::FdmInnerValueCalculator> (*_ppFdmLogBasketInnerValue));
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (PLATFORM::Type^ t) : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
#ifdef HANDLE
	_phFdmLogBasketInnerValue = NULL;
#endif
	if (!t->IsSubclassOf(CFdmLogBasketInnerValue::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (QuantLib::Handle<QuantLib::FdmLogBasketInnerValue>& childNative, Object^ owner)  : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
	_phFdmLogBasketInnerValue = &childNative;
	_ppFdmLogBasketInnerValue = &static_cast<boost::shared_ptr<QuantLib::FdmLogBasketInnerValue>>(childNative.currentLink());
    _ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (boost::dynamic_pointer_cast<QuantLib::FdmInnerValueCalculator> (*_ppFdmLogBasketInnerValue));
    _FdmLogBasketInnerValueOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (QuantLib::Handle<QuantLib::FdmLogBasketInnerValue> childNative)  : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
	_phFdmLogBasketInnerValue = &childNative;
	_ppFdmLogBasketInnerValue = &static_cast<boost::shared_ptr<QuantLib::FdmLogBasketInnerValue>>(childNative.currentLink());
    _ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (boost::dynamic_pointer_cast<QuantLib::FdmInnerValueCalculator> (*_ppFdmLogBasketInnerValue));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::CFdmLogBasketInnerValue (QuantLib::FdmLogBasketInnerValue childNative)  : CFdmInnerValueCalculator(CFdmLogBasketInnerValue::typeid)
{
#ifdef HANDLE
	_phFdmLogBasketInnerValue = NULL;
#endif
	_ppFdmLogBasketInnerValue = new boost::shared_ptr<QuantLib::FdmLogBasketInnerValue> (new QuantLib::FdmLogBasketInnerValue (childNative));
    _ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (boost::dynamic_pointer_cast<QuantLib::FdmInnerValueCalculator> (*_ppFdmLogBasketInnerValue));
}
#endif

Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::~CFdmLogBasketInnerValue ()
{
    if (_ppFdmLogBasketInnerValue != NULL)
    {
	    delete _ppFdmLogBasketInnerValue;
        _ppFdmLogBasketInnerValue = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::!CFdmLogBasketInnerValue ()
{
    if (_ppFdmLogBasketInnerValue != NULL)
    {
	    delete _ppFdmLogBasketInnerValue;
    }
}
QuantLib::FdmLogBasketInnerValue& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::GetReference ()
{
    if (_ppFdmLogBasketInnerValue == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmLogBasketInnerValue;
}
boost::shared_ptr<QuantLib::FdmLogBasketInnerValue>& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::GetShared ()
{
    if (_ppFdmLogBasketInnerValue == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmLogBasketInnerValue;
}
QuantLib::FdmLogBasketInnerValue* Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::GetPointer ()
{
    if (_ppFdmLogBasketInnerValue == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmLogBasketInnerValue;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmLogBasketInnerValue>& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::GetHandle ()
{
	if (_phFdmLogBasketInnerValue == NULL)
	{
		_phFdmLogBasketInnerValue = new Handle<QuantLib::FdmLogBasketInnerValue> (*_ppFdmLogBasketInnerValue);
	}
	return *_phFdmLogBasketInnerValue;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::HasNative () 
{
	return (_ppFdmLogBasketInnerValue != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::AvgInnerValue (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iter, Double t)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmLogBasketInnerValue)->avgInnerValue ( _iter,  _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
Double Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue::InnerValue (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iter, Double prm1)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
        QuantLib::Time _prm1 = (QuantLib::Time)ValueHelper::Convert (prm1); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmLogBasketInnerValue)->innerValue ( _iter,  _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Utilities::IFdmLogBasketInnerValue^ Cephei::QL::Methods::Finitedifferences::Utilities::CFdmLogBasketInnerValue_Factory::Create (Cephei::QL::Instruments::IBasketPayoff^ payoff, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher)
{
    return REFNEW CFdmLogBasketInnerValue ( payoff,  mesher);
}
